package es.index.util;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Properties;

/**
 * @author BeiBei 生产文档ID
 */
public final class ProduceIndexID {

	private static final ProduceIndexID _indexID = new ProduceIndexID();

	private static InetAddress _LOCAL_HOST = null;

	/** 索引前缀 */
	private static String _INDEX_PREFIX = "";
	private static String _SAVE_PREFIX = "$startindex=";

	private static long _INDEX_ID = 0;
	private static String _FILE_PATH = "";
	private static LockFile _lockFile = null;
	private static String _PATH = "indexid.config";
	private static File _FILE = null;

	private ProduceIndexID() {
	}

	public static final ProduceIndexID getInstance() {
		return _indexID;
	}

	public static final void init() {
		final String esPath = System.getProperty("es.index.config");
		_FILE_PATH = esPath + _PATH;

		Properties props = System.getProperties();
		_INDEX_PREFIX = props.getProperty("user.name");
		// System.out.println("Long 最大值:" + Long.MAX_VALUE);
		// System.out.println("文件路径:" + _FILE_PATH);
		// System.out.println("用户的账户名称:" + _INDEX_PREFIX);

		_FILE = new File(_FILE_PATH);
		if (!_FILE.exists()) {
			try {
				_FILE.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		_lockFile = new LockFile(_FILE);

		try {
			_LOCAL_HOST = InetAddress.getLocalHost();
			NetworkInterface byInetAddress;
			byInetAddress = NetworkInterface.getByInetAddress(_LOCAL_HOST);
			byte[] hardwareAddress = byInetAddress.getHardwareAddress();

			StringBuffer mac = new StringBuffer();
			byte currentByte;
			boolean first = false;
			for (byte b : hardwareAddress) {
				if (first)
					mac.append("-");
				currentByte = (byte) ((b & 240) >> 4);
				mac.append(Integer.toHexString(currentByte));
				currentByte = (byte) (b & 15);
				mac.append(Integer.toHexString(currentByte));
				first = true;
			}
			_INDEX_PREFIX = mac.toString().toUpperCase();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (SocketException e) {
			e.printStackTrace();
		}
		_INDEX_PREFIX = _INDEX_PREFIX.replaceAll("_|-", "");
		// System.out.println("索引前缀:" + _INDEX_PREFIX);

		String tempString = _lockFile.readLine();
		if (tempString == null) {
			_INDEX_ID = 0;
			saveIndexStartID();
		} else {
			// System.out.println(tempString);
			String lindex = new String(tempString.substring(tempString.indexOf("=") + 1));
			// System.out.println(lindex);
			_INDEX_ID = Math.abs(Long.parseLong(lindex));
			// System.out.println(tempString);
		}
	}

	public static final void release() {
		saveIndexStartID();
		if (_lockFile != null) {
			_lockFile.release();
			_lockFile.close();
		}
		_lockFile = null;
	}

	public static final void reset() {
		_INDEX_ID = 0;
		saveIndexStartID();
	}

	public static final String getFiledID() {
		final String id = _INDEX_PREFIX + "_" + _INDEX_ID;
		_INDEX_ID++;
		saveIndexStartID();
		// System.out.println(id);
		return id;
	}

	public static final String getIdPrefix() {
		return _INDEX_PREFIX;
	}

	private static final void saveIndexStartID() {
		final String save = _SAVE_PREFIX + _INDEX_ID;
		// System.err.println(save);
		if (_lockFile != null) _lockFile.write(save);
	}
}
